import pydicom
import argparse

def read_dicom_attributes(file_path):
    dicom_file = pydicom.dcmread(file_path, force=True)
    attributes = {}

    for elem in dicom_file:
        if elem.VR != 'SQ':
            attributes[elem.name] = elem.value
    
    return attributes

if __name__ == '__main__':
    warnings.filterwarnings("ignore", category=UserWarning, message="The value length .* exceeds the maximum length .* allowed for VR SH")

    parser = argparse.ArgumentParser(description='Read DICOM attributes from a file.')
    parser.add_argument('file', type=str, help='Path to the DICOM file')
    args = parser.parse_args()

    file_path = args.file
    dicom_attributes = read_dicom_attributes(file_path)
    
    # 手动指定字符集为 GB18030 或 GB2312
    patient_name = dicom_attributes["Patient's Name"]
    try:
        patient_name = patient_name.decode('gb18030')
    except AttributeError:
        # 如果已经是字符串类型，则跳过解码
        pass
    
    print(f'{patient_name}')
